﻿

using System;
using System.Data;
using Model;
namespace Dal
{
	public class PaymentsMethodDal
	{
		public PaymentsMethodDal()
		{
		}
 		#region Insert
 		public static int Insert(PaymentsMethod paymentsMethod)
		{	
			string sql = @"Insert Into PaymentsMethod (
					PName,
					PCode,
					LogoPath,
					AdditionalFee,
					ExtVar,
					Account,
					Description,
					IsInvocation,
					AccountKey,
					MerchantNo,
					Gid,
					GUrl,
					InTime,
					TUrl
				) values('{0}','{1}','{2}',{3},'{4}','{5}','{6}',{7},'{8}','{9}',{10},'{11}','{12}','{13}')";
			sql = string.Format(sql,
					SqlHelper.String(paymentsMethod.PName),
					SqlHelper.String(paymentsMethod.PCode),
					SqlHelper.String(paymentsMethod.LogoPath),
					paymentsMethod.AdditionalFee,
					SqlHelper.String(paymentsMethod.ExtVar),
					SqlHelper.String(paymentsMethod.Account),
					paymentsMethod.Description,
					paymentsMethod.IsInvocation,
					SqlHelper.String(paymentsMethod.AccountKey),
					SqlHelper.String(paymentsMethod.MerchantNo),
					paymentsMethod.Gid,
					SqlHelper.String(paymentsMethod.GUrl),
					paymentsMethod.InTime,
					SqlHelper.String(paymentsMethod.TUrl)
				);
			return SqlHelper.ExecSql(sql);
		}
		#endregion
		
		#region Update
 		public static int Update(PaymentsMethod paymentsMethod)
		{	
			string sql = @"Update PaymentsMethod set 

					PName= '{0}',
					PCode= '{1}',
					LogoPath= '{2}',
					AdditionalFee= {3},
					ExtVar= '{4}',
					Account= '{5}',
					Description= '{6}',
					IsInvocation= {7},
					AccountKey= '{8}',
					MerchantNo= '{9}',
					Gid= {10},
					GUrl= '{11}',
					InTime= '{12}',
					TUrl= '{13}'
				where

					PId= {14}";
			sql = string.Format(sql,
					SqlHelper.String(paymentsMethod.PName),
					SqlHelper.String(paymentsMethod.PCode),
					SqlHelper.String(paymentsMethod.LogoPath),
					paymentsMethod.AdditionalFee,
					SqlHelper.String(paymentsMethod.ExtVar),
					SqlHelper.String(paymentsMethod.Account),
					paymentsMethod.Description,
					paymentsMethod.IsInvocation,
					SqlHelper.String(paymentsMethod.AccountKey),
					SqlHelper.String(paymentsMethod.MerchantNo),
					paymentsMethod.Gid,
					SqlHelper.String(paymentsMethod.GUrl),
					paymentsMethod.InTime,
					SqlHelper.String(paymentsMethod.TUrl),
					paymentsMethod.PId);
			return SqlHelper.ExecSql(sql);
		}
		#endregion
 
 

 		#region Delete
 		public static int Delete(Int32 pId)
		{
		
			string sql = @"Delete from PaymentsMethod where 
					PId= {0}";
			sql = string.Format(sql,
					pId
);
			return SqlHelper.ExecSql(sql);
		} 
		#endregion
 
 		#region Select
 		
 		public static DataSet Select()
		{
			string sql = "select a.* from PaymentsMethod a "; 
			return SqlHelper.Select(sql);
		}
				
		public static PaymentsMethod Select(Int32 pId)
		{
			PaymentsMethod paymentsMethod = null;
			string sql = @"select a.* from PaymentsMethod a  where
					a.PId= {0}
";
			sql = string.Format(sql,
					pId
);
			DataSet ds = SqlHelper.Select(sql);
			if(SqlHelper.HasRow(ds))
				paymentsMethod = new PaymentsMethod(ds.Tables[0].Rows[0]);					
			return paymentsMethod;
		}
		
		public static DataSet Select(string where)
		{ 
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;
			string sql = "select a.* from PaymentsMethod a  where 1=1 " + where ;
			return SqlHelper.Select(sql);
		}
		
		public static DataSet SelectForList(string where,int pageIndex,int pageSize,bool bCount,out int count )
		{
			count = 0;
						
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;
			string sql = "select a.* from PaymentsMethod a where 1=1 " + where;
                
			return SqlHelper.Select(sql,pageSize,pageIndex,bCount,ref count);
		}		
		public static DataSet Select(string where,int pageIndex,int pageSize,bool bCount,out int count )
		{
			count = 0;			
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;			
			string sql = "select a.* from PaymentsMethod a  where 1=1 " + where;
			return SqlHelper.Select(sql,pageSize,pageIndex,bCount,ref count);
		}
		#endregion
	}
}
